package com.fin.zw.aiqas.service;

import com.fin.zw.aiqas.entity.InspectionDetail;
import com.fin.zw.aiqas.entity.QInspectionDetail;
import com.fin.zw.aiqas.enums.ReviewStatus;
import com.fin.zw.aiqas.model.request.DetailReviewQueryRequest;
import com.fin.zw.aiqas.model.response.DetailReviewResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.repository.InspectionDetailRepository;
import com.querydsl.core.BooleanBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: ZhangGQ
 * @Date: 2019/09/18
 * Description:
 */
@Service("detailReviewService")
public class DetailReviewService {
    @Autowired
    private InspectionDetailRepository inspectionDetailRepository;

    /**
     * 分页查询
     * @param request
     * @param sessionModel
     * @param pageable
     * @return
     */
    public Page<DetailReviewResponse> fetchPage(DetailReviewQueryRequest request, SessionModel sessionModel, Pageable pageable) {
        QInspectionDetail qInspectionDetail = QInspectionDetail.inspectionDetail;
        BooleanBuilder builder = new BooleanBuilder();
        //只能看自己的
        builder.and(qInspectionDetail.reviewUserInfo.id.eq(sessionModel.getUserId()));
        if (request.getReviewStatus().equals(ReviewStatus.NOT_CHECK)) {
            builder.and(qInspectionDetail.reviewStatus.eq(ReviewStatus.NOT_CHECK));
        } else {
            builder.and(qInspectionDetail.reviewStatus.eq(ReviewStatus.HAVE_REVIEW));
        }
        //客服编号
        if (StringUtils.isNotBlank(request.getServiceCode())) {
            builder.and(qInspectionDetail.serviceCode.eq(request.getServiceCode()));
        }
        //部门
        if (null != request.getDeptId()) {
            builder.and(qInspectionDetail.department.id.eq(request.getDeptId()));
        }
        //质检时间
        if (null != request.getRunDateE() && null != request.getRunDateS()) {
            builder.and(qInspectionDetail.runDate.between(request.getRunDateS(), request.getRunDateE()));
        }
        //通话时长
        if (null != request.getVoiceLengthS() && null != request.getVoiceLengthE()) {
            builder.and(qInspectionDetail.voiceLength.between(request.getVoiceLengthS(), request.getVoiceLengthE()));
        }
        //任务名称
        if (StringUtils.isNotBlank(request.getTaskName())) {
            builder.and(qInspectionDetail.taskInfo.name.like("%".concat(request.getTaskName()).concat("%")));
        }
        //命中规则
        if (StringUtils.isNotBlank(request.getHitRule())) {
            builder.and(qInspectionDetail.hitRule.like("%".concat(request.getHitRule()).concat("%")));
        }
        Page<InspectionDetail> inspectionDetailPage = inspectionDetailRepository.findAll(builder, pageable);
        Page<DetailReviewResponse> responsePage = inspectionDetailPage.map(inspectionDetail -> {
            DetailReviewResponse response = new DetailReviewResponse();
            BeanUtils.copyProperties(inspectionDetail, response);
            return response;
        });
        return responsePage;
    }

}
